package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class GameActivity extends AppCompatActivity {

    private ImageView walk;

    private TextView xx;


    private Button up;
    private Button down;
    private Button left;
    private Button right;

    private float y =0;

    private String state = "stop";

    AnimationDrawable ad;//图像变化
    ObjectAnimator oa;//图像效果变化
    ObjectAnimator ob;//图像效果变化

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        walk = (ImageView) findViewById(R.id.iv1);

        xx = (TextView) findViewById(R.id.textView22);


        oa = ObjectAnimator.ofFloat(walk,"translationY",y,1000);
        oa.setDuration(10000);
        oa.setRepeatMode(ObjectAnimator.RESTART);

        ob = ObjectAnimator.ofFloat(walk,"translationY",y,0);
        ob.setDuration(10000);
        ob.setRepeatMode(ObjectAnimator.RESTART);



        up = (Button) findViewById(R.id.up);
        up.getBackground().setAlpha(100);

        down = (Button) findViewById(R.id.down);
        down.getBackground().setAlpha(100);

        left = (Button) findViewById(R.id.left);
        left.getBackground().setAlpha(100);

        right = (Button) findViewById(R.id.right);
        right.getBackground().setAlpha(100);

        down.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                walk.setBackgroundResource(R.drawable.walk_down);

                ad = (AnimationDrawable) walk.getBackground();

                if(event.getAction() == event.ACTION_DOWN){
                    ad.start();
                    if(state.equals("stop")){
                        oa.start();
                    }else if(state.equals("pause")){
                        oa.resume();
                    }

                } else if(event.getAction() == event.ACTION_UP){
                    ad.stop();
                    oa.pause();
                    state = "pause";
                }

                int[] location = new int[2];
                walk.getLocationOnScreen(location);
                y = location[1];
                xx.setText("当前位置为："+(int)y);

                return false;
            }
        });
        oa = ObjectAnimator.ofFloat(walk,"translationY",y,1000);
        oa.setDuration(10000);
        oa.setRepeatMode(ObjectAnimator.RESTART);

        ob = ObjectAnimator.ofFloat(walk,"translationY",y,0);
        ob.setDuration(10000);
        ob.setRepeatMode(ObjectAnimator.RESTART);

        up.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                walk.setBackgroundResource(R.drawable.walk_up);

                ad = (AnimationDrawable) walk.getBackground();

                if(event.getAction() == event.ACTION_DOWN){
                    ad.start();
                    if(state.equals("stop")){
                        ob.start();
                    }else if(state.equals("pause")){
                        ob.resume();
                        y = event.getY();
                        xx.setText("当前位置为："+(int)y);
                    }
                } else if(event.getAction() == event.ACTION_UP){
                    ad.stop();
                    ob.pause();
                    state = "pause";
                }

                int[] location = new int[2];
                walk.getLocationOnScreen(location);
                y = location[1];
                xx.setText("当前位置为："+(int)y);

                return false;
            }
        });
        oa = ObjectAnimator.ofFloat(walk,"translationY",y,1000);
        oa.setDuration(10000);
        oa.setRepeatMode(ObjectAnimator.RESTART);

        ob = ObjectAnimator.ofFloat(walk,"translationY",y,0);
        ob.setDuration(10000);
        ob.setRepeatMode(ObjectAnimator.RESTART);
    }
}
